<template>
  <div class="main">
    <div class="register-container">
      <div class="register-card">
        <div class="register-left">
          <div class="pic"></div>
        </div>
        <div class="register-right">
          <div class="form">
            <h1 class="title">注册账号</h1>
            <el-form
                class="register-form"
                :model="registerFormModel"
                :rules="rules"
                ref="registerFormRef"
                @keyup.enter="handleRegister"
            >
              <el-form-item prop="phone">
                <el-input
                    v-model="registerFormModel.phone"
                    placeholder="请输入手机号"
                    prefix-icon="Phone"
                    size="large"
                    clearable
                    autocomplete="username"
                />
              </el-form-item>

              <el-form-item prop="password">
                <el-input
                    type="password"
                    v-model="registerFormModel.password"
                    placeholder="请输入密码"
                    prefix-icon="Lock"
                    show-password
                    size="large"
                    autocomplete="new-password"
                />
              </el-form-item>

              <el-form-item prop="confirmPassword">
                <el-input
                    type="password"
                    v-model="registerFormModel.confirmPassword"
                    placeholder="请确认密码"
                    prefix-icon="Lock"
                    show-password
                    size="large"
                    autocomplete="new-password"
                />
              </el-form-item>

              <el-form-item prop="name">
                <el-input
                    v-model="registerFormModel.name"
                    placeholder="请输入姓名"
                    prefix-icon="User"
                    size="large"
                    clearable
                />
              </el-form-item>

              <el-form-item>
                <el-button
                    type="primary"
                    class="register-btn"
                    @click="handleRegister"
                    :loading="loading"
                    size="large"
                    round
                    block
                >
                  {{ loading ? '注册中...' : '注 册' }}
                </el-button>
              </el-form-item>

              <el-form-item>
                <el-button
                    class="login-button"
                    @click="goToLogin"
                    size="large"
                    round
                    block
                >
                  已有账号，立即登录
                </el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
    </div>

    <div class="copyright">
      <p>&copy; 2025 健身馆 版权所有</p>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import api from "@/utils/api"

const router = useRouter()
const loading = ref(false)
const registerFormRef = ref()

// 注册表单数据
const registerFormModel = reactive({
  phone: '',
  password: '',
  confirmPassword: '',
  name: ''
})

// 表单验证规则
const rules = {
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码至少6位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: (rule: any, value: string, callback: any) => {
        if (value !== registerFormModel.password) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
  name: [
    { required: true, message: '请输入姓名', trigger: 'blur' },
    { min: 2, message: '姓名至少2个字符', trigger: 'blur' }
  ]
}

// 提交注册
const handleRegister = async () => {
  if (loading.value) return

  try {
    await registerFormRef.value.validate()
    loading.value = true

    const response = await api({
      url: "/register",
      method: "PUT",
      data: {
        phone: registerFormModel.phone,
        password: registerFormModel.password,
        name: registerFormModel.name
      }
    })

    if (response.code === 200 && response.success) {
      ElMessage.success("注册成功")
      // 注册成功后跳转到登录页
      router.push('/login')
    } else {
      ElMessage.error(response.message || "注册失败")
    }
  } catch (error) {
    console.error('注册失败:', error)
    ElMessage.error("注册过程中发生错误，请稍后重试")
  } finally {
    loading.value = false
  }
}

// 跳转到登录页面
const goToLogin = () => {
  router.push('/login')
}
</script>

<style scoped>
.main {
  height: 100vh;
  background: linear-gradient(rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3)),
  url("@/assets/loginBackground.png") no-repeat center center/cover;
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}

.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  flex: 1;
}

.register-card {
  display: flex;
  width: 900px;
  height: 550px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 15px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.3);
  overflow: hidden;
  backdrop-filter: blur(10px);
}

.register-left {
  flex: 1;
  background: linear-gradient(rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3)),
  url("@/assets/loginBody.jpg") no-repeat center center/cover;
}

.register-right {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.title {
  text-align: center;
  margin-bottom: 30px;
  color: #1e3c72;
  font-size: 28px;
  font-weight: bold;
}

.register-form {
  width: 100%;
}

.register-btn {
  margin-top: 10px;
  font-weight: bold;
  letter-spacing: 2px;
}

.login-button {
  margin-top: 10px;
  font-weight: bold;
  letter-spacing: 2px;
  background: transparent;
  border: 2px solid #409eff;
  color: #409eff;
}

.copyright {
  position: absolute;
  bottom: 20px;
  width: 100%;
  text-align: center;
  color: rgba(255, 255, 255, 0.8);
}

.copyright p {
  margin: 0;
  font-size: 14px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .register-card {
    width: 90%;
    flex-direction: column;
    height: auto;
  }

  .register-left {
    height: 200px;
  }

  .register-right {
    padding: 20px;
  }
}
</style>
